home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / mozilla-firefox / include / xpcom / nsEscape.h < prev    next >
C/C++ Source or Header  |  2006-05-08  |  7KB  |  190 lines

  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Mozilla Public License Version
  6.  * 1.1 (the "License"); you may not use this file except in compliance with
  7.  * the License. You may obtain a copy of the License at
  8.  * http://www.mozilla.org/MPL/
  9.  *
  10.  * Software distributed under the License is distributed on an "AS IS" basis,
  11.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12.  * for the specific language governing rights and limitations under the
  13.  * License.
  14.  *
  15.  * The Original Code is mozilla.org code.
  16.  *
  17.  * The Initial Developer of the Original Code is
  18.  * Netscape Communications Corporation.
  19.  * Portions created by the Initial Developer are Copyright (C) 1998
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s):
  23.  *
  24.  * Alternatively, the contents of this file may be used under the terms of
  25.  * either of the GNU General Public License Version 2 or later (the "GPL"),
  26.  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27.  * in which case the provisions of the GPL or the LGPL are applicable instead
  28.  * of those above. If you wish to allow use of your version of this file only
  29.  * under the terms of either the GPL or the LGPL, and not to allow others to
  30.  * use your version of this file under the terms of the MPL, indicate your
  31.  * decision by deleting the provisions above and replace them with the notice
  32.  * and other provisions required by the GPL or the LGPL. If you do not delete
  33.  * the provisions above, a recipient may use your version of this file under
  34.  * the terms of any one of the MPL, the GPL or the LGPL.
  35.  *
  36.  * ***** END LICENSE BLOCK ***** */
  37.  
  38. /*    First checked in on 98/12/03 by John R. McMullen, derived from net.h/mkparse.c. */
  39.  
  40. #ifndef _ESCAPE_H_
  41. #define _ESCAPE_H_
  42.  
  43. #include "prtypes.h"
  44. #include "nscore.h"
  45. #include "nsError.h"
  46. #include "nsString.h"
  47.  
  48. /**
  49.  * Valid mask values for nsEscape
  50.  */
  51. typedef enum {
  52.      url_All       = 0         /**< %-escape every byte uncondtionally */
  53. ,    url_XAlphas   = PR_BIT(0) /**< Normal escape - leave alphas intact, escape the rest */
  54. ,    url_XPAlphas  = PR_BIT(1) /**< As url_XAlphas, but convert spaces (0x20) to '+' and plus to %2B */
  55. ,    url_Path      = PR_BIT(2) /**< As url_XAlphas, but don't escape slash ('/') */
  56. } nsEscapeMask;
  57.  
  58. #ifdef __cplusplus
  59. extern "C" {
  60. #endif
  61.  
  62. /**
  63.  * Escape the given string according to mask
  64.  * @param str The string to escape
  65.  * @param mask How to escape the string
  66.  * @return A newly allocated escaped string that must be free'd with
  67.  *         nsCRT::free, or null on failure
  68.  */
  69. NS_COM char * nsEscape(const char * str, nsEscapeMask mask);
  70.  
  71. NS_COM char * nsUnescape(char * str);
  72.     /* decode % escaped hex codes into character values,
  73.      * modifies the parameter, returns the same buffer
  74.      */
  75.  
  76. NS_COM PRInt32 nsUnescapeCount (char * str);
  77.     /* decode % escaped hex codes into character values,
  78.      * modifies the parameter buffer, returns the length of the result
  79.      * (result may contain \0's).
  80.      */
  81.  
  82. NS_COM char *
  83. nsEscapeHTML(const char * string);
  84.  
  85. NS_COM PRUnichar *
  86. nsEscapeHTML2(const PRUnichar *aSourceBuffer,
  87.               PRInt32 aSourceBufferLen = -1);
  88.  /*
  89.   * Escape problem char's for HTML display 
  90.   */
  91.  
  92.  
  93. #ifdef __cplusplus
  94. }
  95. #endif
  96.  
  97.  
  98. /**
  99.  * NS_EscapeURL/NS_UnescapeURL constants for |flags| parameter:
  100.  */
  101. enum EscapeMask {
  102.   /** url components **/
  103.   esc_Scheme         = PR_BIT(0),
  104.   esc_Username       = PR_BIT(1),
  105.   esc_Password       = PR_BIT(2),
  106.   esc_Host           = PR_BIT(3),
  107.   esc_Directory      = PR_BIT(4),
  108.   esc_FileBaseName   = PR_BIT(5),
  109.   esc_FileExtension  = PR_BIT(6),
  110.   esc_FilePath       = esc_Directory | esc_FileBaseName | esc_FileExtension,
  111.   esc_Param          = PR_BIT(7),
  112.   esc_Query          = PR_BIT(8),
  113.   esc_Ref            = PR_BIT(9),
  114.   /** special flags **/
  115.   esc_Minimal        = esc_Scheme | esc_Username | esc_Password | esc_Host | esc_FilePath | esc_Param | esc_Query | esc_Ref, 
  116.   esc_Forced         = PR_BIT(10), /* forces escaping of existing escape sequences */
  117.   esc_OnlyASCII      = PR_BIT(11), /* causes non-ascii octets to be skipped */
  118.   esc_OnlyNonASCII   = PR_BIT(12), /* causes _graphic_ ascii octets (0x20-0x7E) 
  119.                                     * to be skipped when escaping. causes all
  120.                                     * ascii octets to be skipped when unescaping */
  121.   esc_AlwaysCopy     = PR_BIT(13), /* copy input to result buf even if escaping is unnecessary */
  122.   esc_Colon          = PR_BIT(14), /* forces escape of colon */
  123.   esc_SkipControl    = PR_BIT(15)  /* skips C0 and DEL from unescaping */
  124. };
  125.  
  126. /**
  127.  * NS_EscapeURL
  128.  *
  129.  * Escapes invalid char's in an URL segment.  Has no side-effect if the URL
  130.  * segment is already escaped.  Otherwise, the escaped URL segment is appended
  131.  * to |result|.
  132.  *
  133.  * @param  str     url segment string
  134.  * @param  len     url segment string length (-1 if unknown)
  135.  * @param  flags   url segment type flag
  136.  * @param  result  result buffer, untouched if part is already escaped
  137.  *
  138.  * @return TRUE if escaping was performed, FALSE otherwise.
  139.  */
  140. NS_COM PRBool NS_EscapeURL(const char *str,
  141.                            PRInt32 len,
  142.                            PRUint32 flags,
  143.                            nsACString &result);
  144.  
  145. /**
  146.  * Expands URL escape sequences... beware embedded null bytes!
  147.  *
  148.  * @param  str     url string to unescape
  149.  * @param  len     length of |str|
  150.  * @param  flags   only esc_OnlyNonASCII, esc_SkipControl and esc_AlwaysCopy 
  151.  *                 are recognized
  152.  * @param  result  result buffer, untouched if |str| is already unescaped
  153.  *
  154.  * @return TRUE if unescaping was performed, FALSE otherwise.
  155.  */
  156. NS_COM PRBool NS_UnescapeURL(const char *str,
  157.                              PRInt32 len,
  158.                              PRUint32 flags,
  159.                              nsACString &result);
  160.  
  161. /** returns resultant string length **/
  162. inline PRInt32 NS_UnescapeURL(char *str) { return nsUnescapeCount(str); }
  163.  
  164. /**
  165.  * string friendly versions...
  166.  */
  167. inline const nsACString &
  168. NS_EscapeURL(const nsASingleFragmentCString &part, PRInt16 partType, nsACString &result) {
  169.     const char *temp;
  170.     if (NS_EscapeURL(part.BeginReading(temp), part.Length(), partType, result))
  171.         return result;
  172.     return part;
  173. }
  174. inline const nsACString &
  175. NS_UnescapeURL(const nsASingleFragmentCString &str, PRUint32 flags, nsACString &result) {
  176.     const char *temp;
  177.     if (NS_UnescapeURL(str.BeginReading(temp), str.Length(), flags, result))
  178.         return result;
  179.     return str;
  180. }
  181. // inline unescape
  182. inline nsAFlatCString &
  183. NS_UnescapeURL(nsAFlatCString &str)
  184. {
  185.     str.SetLength(nsUnescapeCount(str.BeginWriting()));
  186.     return str;
  187. }
  188.  
  189. #endif //  _ESCAPE_H_
  190.